package org.example.server.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.server.bean.AccountBean;
import org.example.server.common.ResultBean;
import org.example.server.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/save")
    public ResultBean save(@RequestBody AccountBean accountBean){
        log.info("/save,用户注册，请求参数为：{}",accountBean);
        try {
            accountService.save(accountBean);
            return new ResultBean(200,"注册成功",null);
        }catch (RuntimeException e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }

    @RequestMapping("/login")
    public ResultBean login(@RequestBody AccountBean accountBean){
        log.info("/login,用户登录，请求参数为：{}",accountBean);
        try {
            accountService.login(accountBean);
            return new ResultBean(200,"登录成功",null);
        }catch (RuntimeException e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }
}
